const path = require('path')
const name = 'Vue Typescript Admin'
const IS_PROD = ['production', 'development'].includes(process.env.NODE_ENV)

// 设置API URL默认值
if (!process.env.VUE_APP_URL) {
  process.env.VUE_APP_URL = 'http://localhost:8080'
}

// 禁用TypeScript类型检查
process.env.FORK_TS_CHECKER_WEBPACK_PLUGIN_DISABLE = 'true'
process.env.TS_SKIP_TYPECHECK = 'true'

module.exports = {
  'publicPath': process.env.NODE_ENV === 'production' ? './' : '/', // TODO: Remember to change this to fit your need
  'lintOnSave': false, // 关闭lint检查
  'pwa': {
    'name': name
  },
  'pluginOptions': {
    'style-resources-loader': {
      'preProcessor': 'scss',
      'patterns': [
        path.resolve(__dirname, 'src/styles/_variables.scss'),
        path.resolve(__dirname, 'src/styles/_mixins.scss')
      ]
    }
  },
  // 开启代理
  devServer: {
    host:'0.0.0.0',
    public: '0.0.0.0:8888', // 本地的ip:端口号
    port: 8888,
    open: true,
    disableHostCheck:true,
    hot:true,//自动保存
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      '/api': {
        target: process.env.VUE_APP_URL,
        ws: false,
        secure: false,
        changeOrigin: true,
        pathRewrite:{
          '^/api':''
        }
      }
    }
  },
  chainWebpack: (config) => {
    config.resolve.symlinks(true) // 修复热更新失效

    // 禁用TypeScript类型检查
    if (config.plugins.has('fork-ts-checker')) {
      config.plugins.delete('fork-ts-checker')
    }

    // 添加TypeScript处理
    config.resolve.extensions
      .add('.ts')
      .add('.tsx')
      .add('.js')
      .add('.jsx')
      .add('.vue')
      .add('.json')

    // 设置别名
    config.resolve.alias
      .set('@', path.resolve('src'))
  },
  configureWebpack: {
    devtool: 'source-map',
    // TypeScript配置
    module: {
      rules: [
        {
          test: /\.ts$/,
          exclude: /node_modules/,
          use: [
            {
              loader: 'babel-loader'
            },
            {
              loader: 'ts-loader',
              options: {
                transpileOnly: true, // 只编译不类型检查
                happyPackMode: true,
                compilerOptions: {
                  noEmit: false,
                  skipLibCheck: true,
                  noImplicitAny: false,
                  sourceMap: false
                }
              }
            }
          ]
        }
      ]
    }
  },
  // 处理依赖库
  transpileDependencies: [
    '@types/node',
    'undici-types',
    'vue-router'
  ],

  css: {
    // 是否使用css分离插件 ExtractTextPlugin
    extract: IS_PROD,
    // 开启 CSS source maps?
    sourceMap: false,
    // css预设器配置项
    loaderOptions: {
    },
    // 启用 CSS modules for all css / pre-processor files.
    modules: false,
  },
};
